package com.dq.rbac.config;

import com.dq.rbac.commons.security.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import javax.annotation.Resource;

/**
 * @project: rbac
 * @ClassName: SpringSecurityConfig 权限统一配置窗口整合
 * @author: dq
 * @creat: 2022/12/26 14:04
 */
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
    @Resource
    private LoginSuccessHandler loginSuccessHandler;
    @Resource
    private LoginFailureHandler loginFailureHandler;
    @Resource
    private MyUserDetailServiceImpl myUserDetailService;
    @Resource
    private JwtAuthenticationEntryPoint jwtAuthenticationEntryPoint;
    @Resource
    private MyLogoutSuccessHandler myLogoutSuccessHandler;

    private static final String URL_WHITELIST[] = {
            "/login",
            "/logout",
            "/captcha",
            "/password",
            "/image/**",
            "/user/resetRootPass"
    };

    //全局加密的方法
    @Bean
    BCryptPasswordEncoder bCryptPasswordEncoder() {
        return new BCryptPasswordEncoder();
    }

    //自己注入我们独自封装的JWT认证 并且在下面的http配置中 自定义过滤器配置
    @Bean
    JwtAuthenticationFilter jwtAuthenticationFilter() throws Exception {
        return new JwtAuthenticationFilter(authenticationManager());
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //从写自定义实现查库操作
        //还有一个默认的加密Bean配置
        auth.userDetailsService(myUserDetailService);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                //开启跨域支持  以及csrf攻击关闭
                .cors()
                .and()
                .csrf().disable()
                //登录配置 成功 成功\失败 登出 自定义处理
                .formLogin()
                .successHandler(loginSuccessHandler)
                .failureHandler(loginFailureHandler)
                .and()
                    .logout()
                    .logoutSuccessHandler(myLogoutSuccessHandler)
                //session 静止配置
                .and()
                .sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS) //无状态 实现前后端分离
                //拦截规则配置 放行一些请求 登录登出 验证码等
                .and()
                .authorizeRequests()
                .antMatchers(URL_WHITELIST).permitAll()  //白名单放行
                .anyRequest().authenticated()  //其他URL认证

                //异常处理配置
                .and()
                .exceptionHandling()
                .authenticationEntryPoint(jwtAuthenticationEntryPoint)
                //自定义配置
                .and()
                .addFilter(jwtAuthenticationFilter());
    }
}
